from vnpy.trader.object import TickData
from tabulate import tabulate

class TrackData():
    
    # 主动开平累计量
    bid_volume: int=0
    ask_volume: int=0

    # 主动开平增量
    add_bid_volume: int=0
    add_ask_volume: int=0

    duokai: int=0
    kongkai: int=0
    duoping: int=0
    kongping: int=0
    b_duokai: int=0
    b_kongkai: int=0
    b_duoping: int=0
    b_kongping: int=0

    # 挂单量
    gua_bid_volume: int =0
    gua_ask_volume: int =0

    # 力量标识
    li_bid: str=""
    li_ask: str=""

    # add_bid_volume_show: bool = False
    # add_ask_volume_show: bool = False

    # 开 或 平 累计
    # ping_bid_volume: float=0
    # ping_ask_volume: float=0

    # old_index: int = 0
    # old_price: float = 0

    # price:float=0
    # bid_open: float = 0
    # bid_close: float = 0
    # ask_open: float = 0
    # ask_close: float = 0
    # bid_revoke: float = 0
    # ask_revoke: float = 0

    # turnover: float = 0
    # open_interest: float = 0
    # volume: float = 0

    # last_price: float = 0
    # # last_volume: float = 0

    # limit_up: float = 0
    # limit_down: float = 0
    # open_price: float = 0
    # high_price: float = 0
    # low_price: float = 0
    # pre_close: float = 0

    # bid_price_1: float = 0
    # ask_price_1: float = 0
    # bid_volume_1: float = 0
    # ask_volume_1: float = 0

    def __init__(self,price) -> None:
        self.price=price

    # def __repr__(self):
    #     return f"price bid   ask   \n{self.price:6f},{self.bid_volume:6f},{self.ask_volume:6f}\n"

trackList:list[TrackData]=[]
index0_price:int=0

open_price:int=0
pre_close:int=0
high_price:int=0
low_price :int=0
header=["  P   ","F","B","+ ","A","+ ","F","G","G",""]


# old_bid_price: float = 0
# old_ask_price: float = 0

# old_volume: float =0
# old_turnover: float = 0
# old_open_interest: float = 0

def newTrack(price):
    global index0_price
    # price_int=int(price)
    if len(trackList)==0:
        index0_price=price
        trackList.append(TrackData(price))
    else:
        # index=price_int-TrackData.index_diff
        while price>trackList[0].price:
            trackList.insert(0,TrackData(trackList[0].price+1))
            index0_price=price
        while price<trackList[len(trackList)-1].price:
            trackList.append(TrackData(trackList[len(trackList)-1].price-1))

def setTrack(price,bid_volume:int=0,ask_volume:int=0, tick:TickData=None) -> TrackData: 
    newTrack(price)
    t=getTrack(price)
    da=99
    teda=499
    if bid_volume!=0:
        t.add_bid_volume=int(bid_volume)
        t.bid_volume=int(t.bid_volume+bid_volume)
        if bid_volume>teda:
            t.li_bid+="∧"
        elif bid_volume>da:
            i=int(bid_volume/da)
            for j in range(i):
                t.li_bid+="ʌ"
    if ask_volume!=0:
        t.add_ask_volume=int(ask_volume)
        t.ask_volume=int(t.ask_volume+ask_volume)
        if ask_volume>teda:
            t.li_ask+="∨"
        elif ask_volume>da:
            i=int(ask_volume/da)
            for j in range(i):
                t.li_ask+="v"
    # 挂单量
    if t.price==tick.ask_price_1:
        t.gua_ask_volume=tick.ask_volume_1
    if t.price==tick.bid_price_1:
        t.gua_bid_volume=tick.bid_volume_1
    return t
    
def showTrack(tick:TickData):
    bidTotal :int=0
    askTotal:int=0
    data=[]
    for t in trackList:
        bidTotal+=t.bid_volume
        askTotal+=t.ask_volume
        p=f"{t.price:.0f}"
        if tick.bid_price_1==t.price:
            p="B"+p
        elif tick.ask_price_1==t.price:
            p="A"+p
        if t.price==open_price:
            p="O"+p
        if t.price==pre_close:
            p="C"+p
        if t.price==high_price:
            p="H"+p
        if t.price==low_price:
            p="L"+p
        if tick.last_price==t.price:
            p=">"+p
        
        # 拼量串
        bidStr=""
        askStr=""
        if t.add_bid_volume!=0:
            bidStr=f"+{t.add_bid_volume}"
            t.add_bid_volume=0
        if t.add_ask_volume!=0:
            askStr=f"+{t.add_ask_volume}"
            t.add_ask_volume=0
        # if t.li_bid:
        #     bidStr=f"{bidStr} {t.li_bid}"
        # if t.li_ask:
        #     askStr=f"{askStr} {t.li_ask}"

        # print(f"{p:>6s} {t.bid_volume:>6.0f} {bidStr} {t.ask_volume:>6.0f} {askStr} {t.bid_volume-t.ask_volume:.0f}")
        data.append([p,t.li_bid,t.bid_volume,bidStr,t.ask_volume,askStr,t.li_ask,t.gua_bid_volume,t.gua_ask_volume,t.bid_volume-t.ask_volume])

    print(tabulate(data,header,stralign='right'))    
    print(f"{tick.datetime} {bidTotal}-{askTotal}={bidTotal-askTotal}")
    print("")

def getTrack(price) -> TrackData:
    return trackList[int(index0_price-price)]

